package databasetest;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class InputG extends JInternalFrame {
	
	GuestBookMain gbm;
	Connection conn;
	PreparedStatement pstmt=null;
	SelectG s;
	
	private JPanel panel;
	private JLabel label;
	private JTextField user;
	private JLabel lblNewLabel;
	private JTextField title;
	private JPanel panel_1;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTextArea coment;

	public InputG(Connection conn, GuestBookMain gbm) {
		super("방명록 입력",true,true,true,false);
		this.conn=conn;
		this.gbm=gbm;
		setBounds(100, 100, 342, 279);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.SOUTH);
		getContentPane().add(getScrollPane_1(), BorderLayout.CENTER);

	}
	
	public void setD(SelectG s){
		this.s=s;
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getLabel());
			panel.add(getTextField_2());
			panel.add(getLblNewLabel());
			panel.add(getTextField_1_1());
		}
		return panel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("작성자");
		}
		return label;
	}
	private JTextField getTextField_2() {
		if (user == null) {
			user = new JTextField();
			user.setColumns(10);
		}
		return user;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제목");
		}
		return lblNewLabel;
	}
	private JTextField getTextField_1_1() {
		if (title == null) {
			title = new JTextField();
			title.setColumns(10);
		}
		return title;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.add(getBtnNewButton());
		}
		return panel_1;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						String c= user.getText();
						String e= title.getText();
						String m= coment.getText();
						
						String sql ="insert into guestbook(serial,mid,subject,content) values(gb_serial.nextval,?,?,?)";
						pstmt=
								conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setString(2, e);
						pstmt.setString(3, m);
						pstmt.executeUpdate();//입력,수정,삭제;
						
						//pstmt.executeQuery();//조회
						
						s.columnadd();
						
					}catch(Exception e){
						
					}
					
					
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getComent());
		}
		return scrollPane;
	}
	private JTextArea getComent() {
		if (coment == null) {
			coment = new JTextArea();
		}
		return coment;
	}
}
